<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>

<label for="p">大素数P：</label><input type="text" id="p"><br>
<label for="g">生成元g：</label><input type="text" id="g"><br>
<button id="init">生成p和g</button><br>
<label for="x">随机数X：</label><input type="text" id="x"><br>
<label for="y">随机数Y：</label><input type="text" id="y"><br>
<button id="get_random">生成随机数x和y</button><br>
<label for="yb">对方的Y：</label><input type="text" id="yb"><br>
<label for="k">密钥K：</label><input type="text" id="k"><br>
<button id="get_key">生成密钥</button><br>
<label for="plain">明文：</label><input type="text" id="plain"><br>
<label for="encry">密文：</label><label id="encry"></label><br>
<button id="enc">加密</button><br>
<label for="encry2">密文：</label><input type="text" id="encry2"><br>
<label for="plain2">明文：</label><label id="plain2"></label><br>
<button id="dec">解密</button>
</body>

<script type="text/javascript" src="BigInt.js"></script>
<script type="text/javascript" src="jquery.min.js"></script>


<script type="text/javascript" src="aes.js"></script>
<script type="text/javascript" src="../components/mode-ecb.js"></script>

<script type="text/javascript">
	let one=biFromHex('1');//常数1
	let two=biFromHex('2');//常数2
	//计算生成元
	function get_g(P,Q)
	{
		var g=two;//设置初始值为2
		while(biToHex(biPowMod(g,two,P))==biToHex(one)||biToHex(biPowMod(g,Q,P))==biToHex(one))	//g^2 mod P==1|| g^Q mod P==1
		{
			g=biAdd(g,one);	//g++
		}
		return g;
		//while(biPowMod()||biToHex(biPowMod(g,Q,P))
	}
	//获取p
	function get_p()
	{
		//这里需要随机生成k
		return biFromHex('EBEC3D2016842A1AD7E553E735E1EB6F');
	}
	//计算Q
	function get_q(P)
	{
		//alert(P)
		Q=biSubtract(P,one); //P-1
		Q=biDivide(Q,two);	//(P-1)/2
		return Q;
	}
	//获取随机数x
	function get_x(len)
	{
		res=""
		for(var i=0;i<len;i++)
		{
			var id=Math.ceil(Math.random()*16)-1;
			res+=hexToChar[id];	//拼接一位
		}
		return biFromHex(res);//返回大数x
	}
	//计算y
	function get_y(g,X,P)
	{
		var Y=biPowMod(g,X,P);
		return Y;
	}
	//计算K
	function get_k(X,Yb,P)
	{
		return biPowMod(Yb,X,P);
	}
	//加密
	function aes_enc(plain,k)
	{
		var key = CryptoJS.enc.Utf8.parse(k);	
 
		 var srcs = CryptoJS.enc.Utf8.parse(plain);
		 var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
         return encrypted.toString();
	}
	//解密
	function aes_dec(encrypt,k)
	{
	    var key = CryptoJS.enc.Utf8.parse(k);	
 
		 var decrypt = CryptoJS.AES.decrypt(encrypt, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
		 return CryptoJS.enc.Utf8.stringify(decrypt).toString();
	}
	//十六进制字符转换数字
	function hex_to_value(c)
	{
		//console.log(c)
		var v=c.charCodeAt(0);
		if(v>=48&&v<=58)
			return c-'0';
		else if(v>=65&&v<=91)
			return v-65+10;
		else
			return v-97+10;
	}
	//获取十六进制字串对应的字符串
	function get_hex_to_char_str(str)
	{
		var res="";
		for(var i=0;i<32;i+=2)
		{
			var value=hex_to_value(str[i])*16+hex_to_value(str[i+1]);
			//console.log("value "+i+"is"+value);
			res+=String.fromCharCode(value)

		}
		return res;
	}
	$('#init').click(function(){
		//alert('dd')
		var p=get_p();
		$('#p').val(biToHex(p));
		$('#g').val(biToHex(get_g(p,get_q(p))));
	});
	$('#get_random').click(function(){
		p=biFromHex($('#p').val());	//获取p
		g=biFromHex($('#g').val());	//获取g
		var x=get_x(16);	//生成x
		var y=get_y(g,x,p);	//生成y
		$('#x').val(biToHex(x));
		$('#y').val(biToHex(y));
	});
	$('#get_key').click(function(){
		P=biFromHex($('#p').val());
		X=biFromHex($('#x').val());	//获取x
		Yb=biFromHex($('#yb').val());	//获取yb
		K=get_k(X,Yb,P);	//计算K
		$('#k').val(biToHex(K));
	})
	$('#enc').click(function(){
		var plain=$('#plain').val();
		var key=get_hex_to_char_str($('#k').val())
		
		//var temp="";
		//console.log(key.length)
		//for(var i=0;i<key.length;i++)
		//{
		//	console.log(key[i].charCodeAt(0))
		//}

		var encrytext=aes_enc(plain,key)
		$('#encry').text(encrytext)
	})
	
	$('#dec').click(function(){
		var encry=$('#encry2').val();
		var key=get_hex_to_char_str($('#k').val())
		var plain=aes_dec(encry,key)
		$('#plain2').text(plain)
	})
</script>
</html>
